# -*- coding:utf-8 -*-
from scrapy.linkextractors.lxmlhtml import LxmlLinkExtractor
from scrapy.spiders import CrawlSpider
from scrapy.spiders import Rule

from PhenixScrapy.items import MmonlyItem


class MmonlySpider(CrawlSpider):
    """
    https://www.mmonly.cc/mmtp/
    https://www.mmonly.cc/sgtp/
    https://www.mmonly.cc/wmtp/
    https://www.mmonly.cc/ktmh/
    https://www.mmonly.cc/gxtp/
    https://www.mmonly.cc/gqbz/

    """
    name = 'mmonly'
    allowed_domains = ['mmonly.cc']
    start_urls = [
        # 'https://www.mmonly.cc/mmtp/',
        'https://www.mmonly.cc/mmtp/xgmn/',
        'https://www.mmonly.cc/mmtp/swmn/',
        'https://www.mmonly.cc/mmtp/hgmn/',
        'https://www.mmonly.cc/mmtp/bjnm/n',
        'https://www.mmonly.cc/mmtp/nymn/',
        'https://www.mmonly.cc/mmtp/qcmn/',
        'https://www.mmonly.cc/mmtp/ctmn/',
        'https://www.mmonly.cc/mmtp/mnmx/',
        'https://www.mmonly.cc/mmtp/jpmn/',
        'https://www.mmonly.cc/mmtp/wgmn/',
        # 'https://www.mmonly.cc/sgtp/',
        'https://www.mmonly.cc/sgtp/sgmx/',
        'https://www.mmonly.cc/sgtp/jrsg/',
        'https://www.mmonly.cc/sgtp/omsg/',
        'https://www.mmonly.cc/sgtp/dmsg/',
        'https://www.mmonly.cc/sgtp/hgsg/',
        'https://www.mmonly.cc/sgtp/xzsg/',
        # 'https://www.mmonly.cc/wmtp/',
        'https://www.mmonly.cc/wmtp/fjtp/',
        'https://www.mmonly.cc/wmtp/qltp/',
        'https://www.mmonly.cc/wmtp/yjtp/',
        'https://www.mmonly.cc/wmtp/wmwz/',
        'https://www.mmonly.cc/wmtp/wmbj/',
        'https://www.mmonly.cc/wmtp/wmhs/',
        'https://www.mmonly.cc/wmtp/wmxz/',
        # 'https://www.mmonly.cc/ktmh/',
        'https://www.mmonly.cc/ktmh/dmmn/',
        'https://www.mmonly.cc/ktmh/xedm/',
        'https://www.mmonly.cc/ktmh/mhrw/',
        'https://www.mmonly.cc/ktmh/dmmt/',
        'https://www.mmonly.cc/ktmh/qbrw/',
        'https://www.mmonly.cc/ktmh/hzw/',
        'https://www.mmonly.cc/ktmh/hyrz/',
        # 'https://www.mmonly.cc/gxtp/',
        'https://www.mmonly.cc/gxtp/gxmh/',
        'https://www.mmonly.cc/gxtp/dtgxtp/',
        'https://www.mmonly.cc/gxtp/bxjt/',
        'https://www.mmonly.cc/gxtp/nhtp/',
        # 'https://www.mmonly.cc/gqbz/',
        'https://www.mmonly.cc/gqbz/dtbz',
        'https://www.mmonly.cc/gqbz/mnbz',
        'https://www.mmonly.cc/gqbz/fjbz',
        'https://www.mmonly.cc/gqbz/dmbz',
        'https://www.mmonly.cc/gqbz/wmbz',
        'https://www.mmonly.cc/gqbz/wzbz',
        'https://www.mmonly.cc/gqbz/csbz',
    ]
    rules = (
        Rule(LxmlLinkExtractor(allow=('list(_\d{1,2}){2}',)), callback='parse_item', follow=True),
    )

    @staticmethod
    def parse_item(response):
        for selector in response.xpath('//div[@class="img"]//img'):
            item = MmonlyItem()
            item['type'] = response.xpath('//h1[@class="ListTitleH2"]/text()').extract()[0]
            item['title'] = selector.xpath('@alt').extract()[0]
            item['src'] = selector.xpath('@src').extract()[0]
            yield item

            # def parse(self, response):
            #     for selector in response.xpath('//div[@class="img"]//img'):
            #         item = MmonlyItem()
            #         item['title'] = selector.xpath('@alt').extract()[0]
            #         item['src'] = selector.xpath('@src').extract()[0]
            #
            #         print "---------parse---", type(item['title']), "---", type(item['src'])
            #         yield item
